package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SetCharacterEncodingFilter implements Filter {
	
	private String encodingString = null;
	
	public SetCharacterEncodingFilter() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		//转换  
		  HttpServletRequest request = (HttpServletRequest)arg0;  
		  HttpServletResponse response = (HttpServletResponse)arg1;  
		    
		  /* 
		   * 判断在web.xml文件中是否配置了编码格式的信息 
		   * 如果为空，则设置编码格式为配置文件中的编码格式 
		   * 否则编码格式设置为GBK 
		   */  
		  if(this.encodingString != null && !this.encodingString.equals("")){  
		   request.setCharacterEncoding(this.encodingString);  
		   response.setCharacterEncoding(this.encodingString);  
		  }else{  
		   request.setCharacterEncoding("UTF-8");  
		   response.setCharacterEncoding("UTF-8");  
		   
		   /* 
		    * 使用doFilter方法调用链中的下一个过滤器或目标资源（servlet或JSP页面）。 
		    * chain.doFilter处理过滤器的其余部分（如果有的话），最终处理请求的servlet或JSP页面。 
		    */  
		   arg2.doFilter(request, response); 
		  }  

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		this.encodingString = arg0.getInitParameter("encode");

	}

}
